<style>
  .el-table label {
    text-align: center;
  }
</style>
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm"
             size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-date-picker
        v-model="value2"
        type="daterange"
        value-format="yyyy-MM-dd"
        align="right"
        unlink-panels
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        :picker-options="pickerOptions">
      </el-date-picker>
      <el-form-item style="margin-left: 30px">
        <el-button type="primary" icon="el-icon-search" size="medium" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="medium" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="historyEmailList"
      style="width: 100%">
      <el-table-column prop="resumeName" label="姓名"></el-table-column>
      <el-table-column prop="emailMotif" label="邮件主题"></el-table-column>
      <el-table-column prop="mailbox" label="邮箱"></el-table-column>
      <el-table-column prop="sendTime" label="发送时间">
        <template scope="scope">
          <span>{{ parseTime(scope.row.sendTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-user"
            @click="showInfo(scope.row)"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="deleteOne(scope.row.id)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="50%" >
      <span v-html="emailContent"></span>
    </el-dialog>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>


<script>
  import {selectList, deleteHistoryEmail, selectTemplateById} from "@/api/system/historyEmail";
  import Template from "@/views/system/email/ItoTemplate";

  export default {
    name: 'historyEmail',
    components: {Template},
    data() {
      return {
        dialogVisible : false,
        showSearch: true,
        historyEmailList: [],
        emailContent: '',
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          startTime: null,
          endTime: null
        },
        value2: '',
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
        // 总条数
        total: 0
      }
    },
    created() {
      this.getList()
    },
    methods: {
      showInfo(row) {
        selectTemplateById(row.id).then(res => {
          this.emailContent = res.data.emailContent
          this.dialogVisible = true
        })
      },
      /** 搜索按钮操作 */
      handleQuery() {
        if (this.value2 != '' && this.value2 != null) {
          this.queryParams.startTime = this.value2[0];
          this.queryParams.endTime = this.value2[1];
        }
        if (this.value2 == '' || this.value2 == null) {
          this.queryParams.startTime = null;
          this.queryParams.endTime = null;
        }
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.value2 = ""
        this.queryParams = {
          pageNum: 1,
          pageSize: 10,
        }
        this.resetForm("queryForm");
        this.handleQuery();
      },
      getList() {
        selectList(this.queryParams).then(res => {
          console.log(res.rows)
          this.historyEmailList = res.rows
          this.total = res.total;
        })
      },
      deleteOne(id) {
        this.$modal.confirm('您确定要删除吗？').then(function () {
          return deleteHistoryEmail(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {

        });
      }
    }
  }
</script>
